package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@Tag(name = "品牌管模块")
public class BrandController extends BaseController {
    @Resource
    private BrandService brandService;

    @Operation(summary = "查询所有品牌")
    @GetMapping
    public AjaxResult listAll() {//brand继承的BaseEntity中的searchValue属性用来接收前端提交的查询参数
        List<Brand> list = brandService.list();
        return success(list);
    }

    @Operation(summary = "条件查询品牌分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {//brand继承的BaseEntity中的searchValue属性用来接收前端提交的查询参数
        startPage();
        List<Brand> list = brandService.selectRoleList(brand);
        return getDataTable(list);
    }

    @Operation(summary = "新增品牌")
    @PostMapping  //@Validated表示对Brand进行效验，按照Brand中使用的效验注解效验
    public AjaxResult add(@Validated @RequestBody Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            //品牌名称被占用
            return error("新增品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.insertBrand(brand));
    }


    /*@Operation(summary = "删除品牌")
    @DeleteMapping("{brandId}")
    public AjaxResult remove(@PathVariable Long brandId){
        return toAjax(brandService.removeById(brandId));
    }*/

    @Operation(summary = "批量删除品牌") //路径参数：表示参数值必须
    @DeleteMapping("{brandIds}") //  brand/1,2,3      brand?id=1,2,3
    public AjaxResult remove(@PathVariable List<Long>brandIds){
        return toAjax(brandService.removeBrandByIds(brandIds));
    }

    @Operation(summary = "查询品牌详情")
    @GetMapping("{brandId}")
    public AjaxResult getById(@PathVariable Long brandId) {
        return success(brandService.removeById(brandId));
    }

    @Operation(summary = "更新品牌")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Brand brand){
        if(!brandService.checkBrandNameUnique(brand)){
            //品牌名称被占用
            return error("更新品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateBrand(brand));
    }
}